package net.huashitong.Project.web;
import com.sirdc.modules.core.web.JsonBaseController;
import com.sirdc.modules.core.web.model.Message;
import com.sirdc.modules.utils.NumberUtils;
import com.sirdc.modules.utils.ObjectUtils;
import net.huashitong.Project.entity.NewProjects;
import net.huashitong.Project.service.NewProjectsService;
import net.huashitong.node.entity.NodeNewschedules;
import net.huashitong.node.service.NodeNewschedulesService;
import net.huashitong.node.utile.DateUtil;
import net.huashitong.user.entity.Collection;
import net.huashitong.user.filter.CollectionFilter;
import net.huashitong.user.service.CollectionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 项目收藏
 */
@Controller
@RequestMapping("/collection")
public class ProjectCollectionController extends JsonBaseController {

    @Autowired
    private CollectionService collectionService;

    @Autowired
    private NewProjectsService newProjectsService;

    @Autowired
    private NodeNewschedulesService nodeNewschedulesService;

    @Override
    protected String getView(String s) {
        return null;
    }

    @ResponseBody
    @RequestMapping(value = "/app/queryCollections",method = RequestMethod.POST)
    public Message queryProjectCollections(@RequestBody CollectionFilter filter){
        //filter需要userid、currentPage
        List<Collection> collections = collectionService.queryByUserId(filter);
        List<Map<String,Object>> resultList = new ArrayList<>();
        for(Collection collection : collections){
            Map<String,Object> map = new HashMap<>();
            NewProjects np = newProjectsService.getById(collection.getProjectId());
            map.put("projectName", np.getName());
            map.put("sysId", np.getSysId());         //项目id
            map.put("resUser", np.getResUser());     //分管领导
            map.put("resDept", np.getResDept());     //责任单位
            map.put("status", np.getStatus());
            map.put("planBeginDate", np.getPlanBeginDate());//开始时间
            List<NodeNewschedules> list2 = nodeNewschedulesService.getNodeInfo(np.getSysId(), "2", "", "1");
            map.put("smallTotal", list2.size());//小节点总数
            int smallFinishTotal = 0;//已完成节点数目
            int ongoingTotal = 0;//进行中
            int unfinishedTotal = 0;//未完成
            int planTimeTotal = 0;//
            String palnFinishTime = "";
            for (NodeNewschedules nd : list2) {
                if ("0".equals(nd.getStatus())) {
                    unfinishedTotal++;
                } else if ("1".equals(nd.getStatus())) {
                    ongoingTotal++;
                } else if ("2".equals(nd.getStatus())) {
                    smallFinishTotal++;
                }
                if (ObjectUtils.isBlank(nd.getPlanTime())) {
                    nd.setPlanTime("0");
                }
                planTimeTotal = planTimeTotal + Integer.parseInt(nd.getPlanTime());
            }
            palnFinishTime = DateUtil.addDays(np.getPlanBeginDate(), planTimeTotal);
            map.put("smallFinishTotal", smallFinishTotal);
            map.put("ongoingTotal", ongoingTotal);
            map.put("unfinishedTotal", unfinishedTotal);
            map.put("planFinishDate", palnFinishTime);
            map.put("percent", NumberUtils.doubleFormat(smallFinishTotal*(1.0)/list2.size(),4));
            map.put("collectId",collection.getSysId());
            resultList.add(map);
        }
        return coverMessage("200", "获取成功", resultList);
    }


    /**
     * 添加收藏
     * @param filter
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/app/addCollections",method = RequestMethod.POST)
    public Message addProjectCollections(@RequestBody CollectionFilter filter){
        //userId、projectId
        String userId = filter.getUserId();
        String projectId = filter.getProjectId();
        Collection collection = new Collection();
        collection.setUserId(userId);
        collection.setProjectId(projectId);
        collectionService.save(collection);
        return coverMessage("200", "保存成功");
    }

    /**
     * 删除收藏
     * @param filter
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/app/deleteCollections",method = RequestMethod.POST)
    public Message delProjectCollections(@RequestBody CollectionFilter filter){
        //userId、projectId
        String projectId = filter.getProjectId();
        String userId = filter.getUserId();
        CollectionFilter filter1 = new CollectionFilter();
        filter1.setProjectId(projectId);
        filter1.setUserId(userId);
        Collection collection = collectionService.getByUserIdAndSome(filter1);
        collectionService.delete(collection);
        return coverMessage("200", "删除成功");
    }


}
